#!/usr/bin/perl
use warnings;
use strict;

 ########################################################################
#                    Oh you were lucky! v0.01                            #
##########################################################################
#
# usage: perl oywl.pl "string"
# will play a video related to the input string
# if no input given, we were happy in those days
# (c) Pieter Colpaert (pieterc)
# License: GPLv2 or later (see gnu.org)
#
# ###notice: this is just a demo app to show it is possible.
#
# FAQ:
# Name? Oh You Were Lucky (OYWL) is the catchphrase of the monty python sketch.
#   if it does not sound familiar, execute: perl oywl.pl without any arguments.
#   in this script you got to have be lucky to watch the video you thought off.
#
# Why the hell did you use perl? I'm a student engineering and I got a test on
#     perl tomorrow. I hope tomorrow I write cleaner code indeed.
#
# Tricks:
#   - put a bash script in /usr/local/bin/ saying: perl ~/oywl.pl "$1"
#          --> this way won't need to type perl oywl.pl
#
  #			--->Another tip, link oywl.pl to /usr/local/bin/oywl that way you don't need to type perl or .pl
  #				(Maybe make an bash script, or another perl script that does this automatically)
# Scheduled features:
#   - rewrite in bash/bussybox or Python. Not sure...
#   - GUI (with a TEXTBOX (wow, technology!)
#   - decent output with title of the video, comments, ...
#   - seek (forward/backward, is possible yet when you return to terminal en perform back)
#
# Known issues
#   - nosound arg does not work for a reason... Anyone? (contact me ;) )
#
# requirements:
#   - mplayer
#   - perl
#   - any other?
#
# #DBG# ==> remove this in entire file if you want more output
#               (M-x replace-regexp <RET> #DBG# <RET> <RET> !)
#
# Author: Pieter Colpaert <pieter.colpaert <aŧ> gmail.com
#

#Make sure to define variables, and try your best not to use Globals (our)
my @mpargs;
my $result;
my $url;
my $html;
my @url;
my $topic;
my $lowkwal;
my $arg;
my $title;
#enable slurpmode
undef $/;
#extra mplayer arguments
@mpargs = ( "-ao sdl", "-cache 10000", "-framedrop"); #useful args: , "-vf rotate=1" 
#DBG##$ARGV[0]= "power rangers";
$topic = $ARGV[0]||"The four yorkshire men";
###Let's find us a video with youtube search
if ( $ARGV[0] ) {
    print "#...Connecting to youtube search page for $ARGV[0]...\n";
    #this is needed for the url won't accept spaces
    $ARGV[0] =~ s/ /+/g;

    #get youtube searchpage
    `wget http://www.youtube.com/results?search_query=$ARGV[0]  --output-document=/tmp/oywl.html 2>/dev/null`;
    open (SEARCH, "/tmp/oywl.html") or die "Search not opened: $!";
    $result = <SEARCH>;
    $result =~ s/.*id="video-url-(.*?)".*/$1/ms;
    #DBG#print $result;
    $result && print "#...succeeded...\n";
}else{
    print "#...No videotopic specified. I will load the video about the Yorkshire men...\n";
}
#when no result found or no argv set, display the four yorkshire men
$result = $result || "Xe1a1wHxTyo";

#Alright, we got a vidID, let's play the bastard

$url = "http://www.youtube.com/watch?v=$result";
print "#...Getting the video file-url... \n";
`wget $url --output-document=/tmp/oywl.html 2>/dev/null`
  ;    #COMMENT THIS OUT FOR DEBUGGING 2>/dev/null

open (HTML, "/tmp/oywl.html") or die "Did I not get an argument? $!";

#load the entire html file
$html = <HTML>;
#setting the topic title
if($html=~ /VIDEO_TITLE': '(.*?)',/){
  $title = $1;
}
#get some different quality url's

$html =~ s/.*?fmt_url_map": "(.*?)", "rv.*/$1/sm;

#filter out hex - values in the urls
$html =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
@url = split /\|/, $html;

#DBG#print join "\n",@url;
#our moko only can handle lowest quality :( (some dirty things came with the url aswel, filtering out: , .* and ")
($lowkwal) = split /,|"/, $url[-1];

#We will use a shell script to load mplayer for us, so we have to escape % \& &
$lowkwal =~ s/([&%])/\\$1/gsm;
#DBG#print $lowkwal;
print "
We will play this video for you:
ID = $result
Topic = $topic
Videotitle = $title

Please feel free when the video is running to use mplayer command in this terminal
#... Buffering your video ...
";
$arg = join " ", @mpargs;

#DBG#print $arg;
`mplayer  $arg  $lowkwal 2>/dev/null`;#hide details
print "Thank you for using Oh You Were Lucky!\n";

